package com.mintandleaf.webkits.validator;

/**
 * Validator fo date data type we are using apache common validator 1.3
 * as main validation library
 * @author teguhh
 */
public class DateValidator extends BaseValidator{

    private String dateFormat;

    /**
     * Construct the date validator using standard
     *  Date format 'dd/MM/YYYY'
     * 
     **/
    public DateValidator(){
        setDateFormat("dd/MM/YYYY");
        setErrorMessage("it's not a valid date");
    }
    /**
     * Contruct the date validator using predefined
     * date format
     * @param paramDateFormat
     */
    public DateValidator(String paramDateFormat,String paramErrorMessage){
        setDateFormat(dateFormat);
        setErrorMessage(paramErrorMessage);
    }

    /**
     * Validate a valu is a date or not, we using strict
     * validation on date format event if the value is a date
     * but format its not the same validation will failed
     * @param value to be validate
     * @return true or false on validation
     */
    @Override
    public boolean validate(Object value) {
        org.apache.commons.validator.DateValidator validator=org.apache.commons.validator.DateValidator.getInstance();
        return validator.isValid(String.valueOf(value), getDateFormat(),true);
    }
    /**
     * Get date format to be validate
     *
     **/
    public String getDateFormat() {
        return dateFormat;
    }

    /**
     *
     * Set date format to validate
     * @param dateFormat
     */
    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }
}
